
function RightMenuRender(tree,id){
	this.tree = tree ;
	this.id = id==null?RightMenuRender.defaultInstanceName:id ;
	this.degree = tree.getDegree() ;
	this.mouseDegree = -1 ;
	this.beginX = 0 ;
	this.widths = null ;
	this.lock = false ;
	return this ;
}

RightMenuRender.defaultInstanceName = "RMRender";

RightMenuRender.prototype.bindById = function () {
    window[this.id] = this;
}

function findRightMenuRender(id) {
	if(id != null){
		return window[id];
	}
    return window[RightMenuRender.defaultInstanceName];
}


RightMenuRender.prototype.init = function(){
	var degree = this.tree.getDegree();
	this.widths = new Array();
	this.heights = new Array();
	for(var i=0;i<degree;i++){
		this.widths[i] = 0 ;
	}
	
	var size = this.tree.nodes.size();
	for(var i=0; i<size; i++){
		var menu = this.tree.nodes.get(i);
		var len = menu.name.length;
		if(len > this.widths[menu.rank]){
			this.widths[menu.rank] = len ;
		}
	}

	for(var i=0;i<degree;i++){
		this.widths[i] = 22*1 + this.widths[i]*12 + 16 ;
	}

	var buffer = new StringBuffer();
	for(var i=0;i<degree;i++){
//		debug(i+":"+this.widths[i]);
		buffer.append("<div id='divDropMenu_").append(i).append("' class='divDropMenu' style='width:"+(this.widths[i]*1+31)+";' onmouseover=\"lockDropMenuDiv(").append(i).append(",'").append(this.id).append("')\" onmouseout=\"hiddenDropMenuDiv(").append(i).append(",'").append(this.id).append("')\">");
		buffer.append("<TABLE cellspacing=0 cellpadding=0 id='dropMenuTable_"+i+"' class='dropMenuTable'>");
		buffer.append("</TABLE>");
		buffer.append("</div>");
	}
	document.body.innerHTML += buffer.toString();
}

RightMenuRender.prototype.show = function(node,event){
	var degree = node.rank ;
	this.mouseDegree = degree ;
	var t = document.getElementById("dropMenuTable_"+degree);
	if(t == null){
		return ;
	}
	this.clearDropMenus(t);

	var size = node.children.size();
	for(var i=0; i<size; i++){
		var menu = node.getChildAt(i);
		if(menu.enable){
			newRow = t.insertRow(t.rows.length);
			newRow.className = "dropMenuOutLine";
			c0 = newRow.insertCell(0);
			c0.className = "colMenuLine";
			c0.innerHTML = this.getMenuHTML(menu);
		}
	}
	this.showDropMenus(degree,event);
}

RightMenuRender.prototype.getMenuHTML = function(menu){
	var tableStyle = "dropMenuItemOut";
	var colStyle = "colDropMenuItemIcon";
	var iconStyle = "menuItemIcon";
	var textStyle = "colDropMenuItemText";
	var subStyle = "colDropMenuSubNoChild";
	var moveStyle = " onmouseover=\"changeMenuItemCss(this);showSubMenus('"+menu.id+"',event,'"+this.id+"')\"";
	var clickEvent = " onclick=\"hiddenDropMenuDivImmediately('"+this.id+"');"+menu.method+"\"";

	var buffer = new StringBuffer();
	if(menu.hasChild()){
		subStyle = "colDropMenuSubHasChild";
	}else{
		if(menu.enable){
			moveStyle = " onmouseover=\"changeMenuItemCss(this);hiddenSubMenus('"+menu.rank+"','"+this.id+"')\"";
		}else{
			moveStyle = " onmouseover=\"hiddenSubMenus('"+menu.rank+"')\"";
		}
	}
	if(!menu.enable){
		tableStyle = "dropMenuItemOutDisabled";
		colStyle = "colDropMenuItemIconDisabled";
		iconStyle = "menuItemIconDisabled";
		textStyle = "colDropMenuItemTextDisabled";
		if(menu.hasChild()){
			subStyle = "colDropMenuSubDisabled";
		}
		clickEvent = "";
		moveStyle = " onmouseover=\"hiddenSubMenus('"+menu.rank+"')\"";
	}

	buffer.append("<table cellspacing=0 cellpadding=0 class='").append(tableStyle).append("'").append(moveStyle).append(clickEvent).append(">");
	buffer.append("<tr>");
	buffer.append("<td class='").append(colStyle).append("'>");
	if(menu.icon != null && menu.icon != ""){
		buffer.append("<img class='").append(iconStyle).append("' src='").append(menu.icon).append("'></img>");
	}else{
		buffer.append("<span style='width:12px;'></span>");
	}
	buffer.append("</td>");

	buffer.append("<td class='").append(textStyle).append("'").append("><div style='width:100%;'>");
	buffer.append(menu.name) ;
	buffer.append("</div></td>");

	buffer.append("<td class='").append(subStyle).append("'><div style='width:16px;'></div></td>");
	
	buffer.append("</tr>");
	buffer.append("</table>");

	return buffer.toString() ;
}

function showSubMenus(id,event,renderId){
	var render = findRightMenuRender(renderId);
	var node = render.tree.findNodeById(id);
	render.show(node,event);
}

var timeoutBar ;

function lockDropMenuDiv(degree,renderId){
	var render = findRightMenuRender(renderId);
	render.mouseDegree = degree ;
	render.lock = true ;
	clearTimeout(timeoutBar);
	clearTimeout(timeoutBar);
}

var hiddenInterval = 500 ;
var currentRenderId ;
function hiddenDropMenuDiv(degree,renderId){
	var render = findRightMenuRender(renderId);
	render.mouseDegree = degree-1 ;
	render.lock = false ;
	currentRenderId = renderId ;
	timeoutBar = setTimeout("hiddenDropMenus()",hiddenInterval);
}

function hiddenSubMenus(n,renderId){
	var render = findRightMenuRender(renderId);
	for(var i=n; i< render.degree ;i++){
		var t = document.getElementById("divDropMenu_"+i);
		if(t == null){
			break ;
		}
		t.style.display = "none";
	}
}

function hiddenDropMenuDivImmediately(renderId){
	var render = findRightMenuRender(renderId);
	render.lock = false ;
	hiddenDropMenus(renderId);
}

function hiddenDropMenus(renderId){
	if(renderId == null){
		renderId = currentRenderId ;
	}
	var render = findRightMenuRender(renderId);
	if(render.lock){
		return ;
	}
	var end = render.tree.getDegree() ;
	for(var i=0;i<end;i++){
		var t = document.getElementById("divDropMenu_"+i);
		t.style.display = "none";
	}
}

RightMenuRender.prototype.clearDropMenus = function(t){
	for(var i=t.rows.length-1 ;i>=0; i--){
		t.deleteRow(i);
	}
}

RightMenuRender.prototype.showDropMenus = function(degree,event){
	var t = document.getElementById("divDropMenu_"+degree);
	t.style.left = this.getPositionX(degree,event);
	t.style.top  = this.getPositionY(degree,event);
	t.style.display = "block";
}

RightMenuRender.prototype.getPositionX = function(degree,event){
//	debug(degree);
	var px = 0;
	if(degree > 0){
		for(var i=0;i<degree;i++){
			px += this.widths[i+1]*1 + 22 ;
		}
		return px*1 + this.beginX ;
	}else{
		var w = document.body.clientWidth ;
		var x = event.clientX ;
		if(x < w/2){
			px = x-2 ;
		}else{
			px = x - this.widths[degree] ;
		}
		this.beginX = px ;
		return px ;
	}
}
RightMenuRender.prototype.getPositionY = function(degree,event){
	var i = document.getElementById("dropMenuTable_"+degree).rows.length ;
	var h = document.body.clientHeight ;
	
	var m = 20 ;

	var y = event.clientY ;	
	var py = 0;
	if(y < h/2){
		py = y - 8;
	}else{
		py = y - ((i * m)/ 2) ;
	}
	py = py - (py % m) ;
	return py ;
}


var preMenuItem ;
function changeMenuItemCss(me){
	if(preMenuItem != null){
		preMenuItem.className = "dropMenuItemOut";
	}
	me.className = 'dropMenuItemOver';
	preMenuItem = me ;
}